package UnionFind;

import java.util.Hashtable;

import BasicTypes.Constants;

public class UnionFindHashTable {
	//private int[] id;
	private Hashtable<Integer, Integer> metrix;
	//public int[] getId() {
	//	return id;
	//}
	//int distance;
	
	
	   public Hashtable<Integer, Integer> getMetrix() {
		return metrix;
	}

	public UnionFindHashTable()
	   {
		   metrix = new Hashtable<Integer, Integer>();
		 // distance = Group * Constants.COLUMESIZE * Constants.SIZE;
	     // id = new int[N];
	     // for (int i = 0; i < N; i++) id[i] = i;
	   }
	   
	   private int root(int i)
	   {
		  //int root = i;
		  if(!metrix.containsKey(i)){
			  metrix.put(i,i);
		  }else{
			  while(i != metrix.get(i)){
		    	  int j = metrix.get(i);
		    	  int q = metrix.get(j);
		    	  metrix.put(i, q);
				  i = metrix.get(i);
			  }
		  }
	      return i;
	   }
	   
	   public boolean find(int p, int q)
	   {
	      return root(p) == root(q);
	   }
	   
	   public void unite(int p, int q)
	   {
	      int i = root(p);
	      int j = root(q);
	      if(i < j){
	    	  metrix.put(j, i);
	      }else{
	    	  metrix.put(i, j);
	      }
	   }
	   
	   public int getRoot(int i){
			  if(!metrix.containsKey(i)){
				  return i;
			  }else{
				  while(i != metrix.get(i)){
			    	  int j = metrix.get(i);
			    	  int q = metrix.get(j);
			    	  metrix.put(i, q);
					  i = metrix.get(i);
				  }
			  }
		      return i;
	   }
	   
	   public void print(){
		   for(int i = 0; i < Constants.SIZE; i++){
			   for(int j = 0; j < Constants.SIZE; j++){
				   if(metrix.containsKey(i* Constants.SIZE + j)){
					   System.out.print(root(i* Constants.SIZE + j));
					   System.out.print("\t");
				   }else{
					   System.out.print(-1);
					   System.out.print("\t");
				   }
				   
			   }
			   System.out.println();
		   }
	   }
}
